package com.htkj.demo

import android.app.Application
import com.htkj.demo.CacheUtil.getInt
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.model.VideoOptionModel
import com.shuyu.gsyvideoplayer.player.IjkPlayerManager
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.shuyu.gsyvideoplayer.player.SystemPlayerManager
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.tencent.mmkv.MMKV
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager
import tv.danmaku.ijk.media.player.IjkMediaPlayer


class APP: Application() {
    override fun onCreate() {
        super.onCreate()
        MMKV.initialize(this.filesDir.absolutePath + "/mmkv")

        val kernel = getInt("kernel", 0)
        val hardware = getInt("hardware", 0)


        when(kernel){
            0->{
                PlayerFactory.setPlayManager(SystemPlayerManager::class.java) //系统模式
            }
            1->{
                PlayerFactory.setPlayManager(Exo2PlayerManager::class.java)  ;//EXO模式
            }
            2->{
                PlayerFactory.setPlayManager(IjkPlayerManager::class.java);//ijk模式
            }
        }
        if(hardware==1){
            /// 打开硬解码
            GSYVideoType.enableMediaCodec()
            GSYVideoType.setRenderType(GSYVideoType.TEXTURE)
            GSYVideoType.enableMediaCodecTexture()
        }

//        GSYVideoType.setShowType(GSYVideoType.SCREEN_MATCH_FULL)
//
//        /// 打开硬解码
//        GSYVideoType.enableMediaCodec()
//        GSYVideoType.setRenderType(GSYVideoType.TEXTURE)
//        GSYVideoType.enableMediaCodecTexture()
//
//        PlayerFactory.setPlayManager(Exo2PlayerManager::class.java)  ;//EXO模式
//
//        // 2. 启用 ExoPlayer 缓存模式
////        CacheFactory.setCacheManager(ExoPlayerCacheManager::class.java)
////        PlayerFactory.setPlayManager(SystemPlayerManager::class.java) //系统模式
//                PlayerFactory.setPlayManager(IjkPlayerManager::class.java);//ijk模式
//        PlayerFactory.setPlayManager(AliPlayerManager.class);//aliplay 内核模式
        val list: MutableList<VideoOptionModel> = ArrayList<VideoOptionModel>()
//        val videoOptionMode04: VideoOptionModel =
//            VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "packet-buffering", 0) //是否开启缓冲
//        list.add(videoOptionMode04)
//        //drop frames when cpu is too slow：0-120
        val videoOptionMode05: VideoOptionModel =
            VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 1) //丢帧,太卡可以尝试丢帧
        list.add(videoOptionMode05)
        val videoOptionMode07: VideoOptionModel =
            VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 48) //默认值48
        list.add(videoOptionMode07)
        val videoOptionMode18: VideoOptionModel =
            VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "enable-accurate-seek", 1)
        list.add(videoOptionMode18)
        GSYVideoManager.instance().setOptionModelList(list)



    }

    override fun onLowMemory() {
        super.onLowMemory()
        //清理缓存
        GSYVideoManager.instance().clearAllDefaultCache(this)
    }



}